<!DOCTYPE html>
<html lang="zh_CN">

<head>
   <#include "../common/header.ftl">

    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.css" type="text/css"/>
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.js"></script>

    <script>

        $(function () {
            //本项目代码
           $.get("/region/getRegionByParentId.do",function (topdate) {
               console.log(topdate);
               $('#treeview').treeview({
                   levels:1,
                   showTags: true,
                   data: [{text:'全部地区',nodes:topdate}],
                   lazyLoad:function (node) {
                       $.get("/region/getRegionByParentId.do",{ParentId:node.id},function (subdate) {
                           $("#treeview").treeview("addNode",[subdate, node]);
                       })
                   },
                   //当节点被选中会触发方法事件
                   onNodeSelected:function (ev,node) {
                       console.log(node);
                       var html="";
                       $.get("/region/getRegionByParentId.do",{ParentId:node.id},function (subdate) {
                           //把查询出来的数据拼接成tr/td
                           console.log(subdate);
                           $.each(subdate,function (index, ele) {
                               //html += "<tr><td>"+(index+1)+"</td><td>"+ele.text+"</tr>";
                               html += "<tr><td>"+(index+1)+"</td><td>"+ele.text+"</td><td><a data-json='"+ele.json+"' href='javascript:;' class='editBtn'>编辑</a></td></tr>";
                           })
                           $("#tbody").html(html);
                       })
                   }

               })
           })


            //动态绑定事件 对地区的编辑按钮进行绑定
            $("#tbody").on("click",".editBtn",function () {
                $("#editForm").clearForm(true);

                var json = $(this).data("json");
                if(json){
                    var parentNode = $("#treeview").treeview('getSelected')[0];

                    $("#editForm input[name='id']").val(json.id);
                    $("#editForm input[name='parentId']").val(json.parentId);
                    $("#editForm input[name='name']").val(json.name);
                    $("#editForm input[name='parentName']").val(parentNode.text);
                    $("#editForm select[name='state']").val(json.state);
                }

                $("#inputModal").modal("show");
            })


            //地区保存操作
            $(".saveBtn").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    if (data.success){
                        window.location.reload();
                    }else{
                        $.messager.alert(data.msg);
                    }
                })
            })


            //地区添加操作
            $("#addRegionBtn").click(function () {
                $("#editForm").clearForm(true);

                var parentNode = $("#treeview").treeview('getSelected')[0];

                if(!parentNode){
                    $.messager.alert("请选中一个地区");
                }

                $("#editForm input[name='parentName']").val(parentNode.text);
                $("#editForm input[name='parentId']").val(parentNode.id);
                $("#inputModal").modal("show");
            })

        })

    </script>

</head>

<body>

<!--模态框-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">地区编辑</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post"
                      id="editForm">
                    <input type="hidden" name="id">
                    <input type="hidden" name="parentId">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parentName" disabled="disabled">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否推荐：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">不推荐</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary saveBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#--设置当前要回显当前菜单,必须在载人菜单前完成设置-->
            <c:set var="menu" value="department"/>
                <#assign menu = "region" >
           <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>

            <!--栅格系统 把上述的9块再拆分成12块-->
            <div class="row">
                <!--折叠列表-->
                <div class="col-sm-4">
                    <div id="treeview"></div>

                </div>

                <div class="col-sm-8">

                    <div class="form-group">
                        <a href="javascript:void(-1);" class="btn btn-success" id="addRegionBtn">添加地区</a>
                    </div>
                    <table class="table table-striped table-hover">
                        <tr>
                            <th>序号</th>
                            <th>名字</th>
                            <th>操作</th>
                        </tr>
                        <tbody id="tbody">

                        </tbody>
                    </table>

                </div>


            </div>


        </div>
    </div>
</div>

<script>

</script>

</body>

</html>